<?php
	session_start();	//Sessione.
	include 'dbconnect.php';	//Connessione DB.
    $json = array('players'=>array(), 'enemies'=>array());	//Json, doppio array player e nemico
	$query = 'SELECT * FROM players WHERE lastupdate > TIMESTAMPADD(MINUTE, -10, NOW()) AND name <> "'.$_GET['name'].'"';	//Cerco players connessi ultimi 10 minuti.
	$result = mysqli_query($link, $query);
    while ($obj = mysqli_fetch_object($result)) {
        array_push($json['players'], array('name'=>$obj->name,'x'=>floatval($obj->x),'y'=>floatval($obj->y),
		'dir'=>intval($obj->dir),'state'=>intval($obj->state),'life'=>floatval($obj->life),'attack'=>floatval($obj->attack),
		'defense'=>floatval($obj->defense), 'xp'=>floatval($obj->xp), 'pa'=>floatval($obj->pa)));
	}
	mysqli_free_result($result);
	$query = "SELECT * FROM enemies WHERE life <> 0";
	$result = mysqli_query($link, $query);
    while ($obj = mysqli_fetch_object($result)) {
        array_push($json['enemies'], array('name'=>$obj->name, 'type'=>$obj->type, 'x'=>floatval($obj->x), 'y'=>floatval($obj->y)));
    }
    mysqli_free_result($result);
    echo json_encode($json);
	$name = $_GET['name'];
	$x    = $_GET['x'];
	$y    = $_GET['y'];
	$dir  = $_GET['dir'];
	$state  = $_GET['state'];
	$life = $_GET['life'];
	$attack = $_GET['attack'];
	$defense = $_GET['defense'];
	$xp = $_GET['xp'];
	$pa = $_GET['pa'];
	
	if(isset($name) && isset($x) && isset($y) && isset($dir) && isset($state) && isset($life) && isset($xp) && isset($pa)){
        mysqli_query($link, 'UPDATE players SET x='.$x.', y ='.$y.', dir = '.$dir.', state = '.$state.',
		life = '.$life.', attack = '.$attack.', defense = '.$defense.', xp = '.$xp.', pa = '.$pa.', lastupdate = NOW() WHERE name="'.$name.'"');
	}
	mysqli_close($link);
?>